package guilayer;

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import controllayer.*;
import dblayer.DBProduct;

/**
 * @author Gruppe 3
 */
public class SupplierPanel extends JPanel {
	private JTable supplierTable;
	private JScrollPane scrollPane;
	private JPanel cardPanel, viewPanel;
	private SupplierCtr sCtr;
  	private static SupplierPanel instance;
  	private JButton btnDeleteSupplier;

	/**
	 * Create the panel.
	 */
	private SupplierPanel() {
		sCtr = new SupplierCtr();
		setLayout(null);
		initTable();
		initSeconPanel();
		initButtons();
	}
	
	public static SupplierPanel getInstance() {
		if(instance == null) {
			instance = new SupplierPanel();
		}
		return instance;
	}
	
	/**
	 * Initialize JTable
	 */
	private void initTable() {
		

		String[][] data = sCtr.supplierToTableArray();
		String[] column = {"ID", "Name", "Address", "Country", "Phone number", "Email"};
		
		supplierTable = new JTable(data, column){
			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int row, int column) {                
	                return false;               
	        }
	    };
		supplierTable.getTableHeader().setReorderingAllowed(false);
		supplierTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		supplierTable.setFillsViewportHeight(true);
		supplierTable.setAutoCreateRowSorter(true);
		supplierTable.setVisible(true);
		scrollPane = new JScrollPane(supplierTable);
        scrollPane.setBounds(10, 11, 445, 400);
        add(scrollPane);
        
        supplierTable.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent event) {
            	try {
            		viewSupplier(Integer.parseInt(supplierTable.getValueAt(supplierTable.getSelectedRow(), 0).toString()));
            	} catch(ArrayIndexOutOfBoundsException e) {
            	} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        });


	}
	
	/**
	 * Initialize the secondPanel
	 */
	private void initSeconPanel() {
		cardPanel = new JPanel();
		cardPanel.setBounds(468, 11, 350, 434);
		add(cardPanel);
		cardPanel.setLayout(new CardLayout(0, 0));
	}
	
	public void initButtons() {
		JButton btnNewSupplier = new JButton("New Supplier");
        btnNewSupplier.setBounds(334, 422, 121, 23);
        add(btnNewSupplier);
        btnNewSupplier.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
					newSupplier();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        }
    });
        
        btnDeleteSupplier = new JButton("Delete Supplier");
    	btnDeleteSupplier.setBounds(203, 422, 121, 23);
    	add(btnDeleteSupplier);
        btnDeleteSupplier.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
					deleteSupplier();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        }
    });
	}
	
	/**
	 * Updates the Table
	 */
	public void updateTable() {
		supplierTable.removeAll();
		this.remove(supplierTable);
		scrollPane.removeAll();
		this.remove(scrollPane);
		initTable();
	}
	
	/**
	 * Changes panel to viewForest
	 * @param forestName
	 * @throws SQLException 
	 */
	public void viewSupplier(int supplierID) throws SQLException {		
		viewPanel = new ViewSupplierPanel(supplierID);
		cardPanel.add(viewPanel);
		CardLayout cl = (CardLayout)(cardPanel.getLayout());
		cl.last(cardPanel);
	}
	
	public void newSupplier() throws SQLException {		
		viewPanel = new NewSupplierPanel();
		cardPanel.add(viewPanel);
		CardLayout cl = (CardLayout)(cardPanel.getLayout());
		cl.last(cardPanel);
	}
	
	/**
	 * Deletes the supplier
	 * @throws SQLException
	 */
	private void deleteSupplier() throws SQLException {
		try {
			int valg = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete the supplier?", 
					"Delete supplier", JOptionPane.YES_NO_OPTION);
			if(valg == JOptionPane.YES_OPTION) {
				int supplierID = Integer.parseInt(supplierTable.getValueAt(supplierTable.getSelectedRow(), 0).toString());
				sCtr.deleteSupplier(supplierID);
				updateTable();
			}	
		}
		catch(IndexOutOfBoundsException e)
		{
			//
		}
	}
}
